package com.popcorn.board.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;
import com.popcorn.board.dao.BoardDAO;


public class BoardListAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		BoardDAO BoardDAO=new BoardDAO();
		List list=new ArrayList();
		
	  	int page=1;
		int limit=10;
		
		if(request.getParameter("page")!=null){
			page=Integer.parseInt(request.getParameter("page"));
		}
		
		int listcount=BoardDAO.getListCount(); //�?리스???��? 받아??
		list = BoardDAO.modelList(page,limit); //리스?��? 받아??
		
		//�??�이�???
   		int maxpage=(int)((double)listcount/limit+0.95); //0.95�??�해???�림 처리 
   		//?�재 ?�이�?�� 보여�??�작 ?�이�?�� (1,11,21 ??
   		int startpage = (((int) ((double)page / 10 + 0.9)) - 1) * 10 + 1;
   		//?�재 ?�이�?�� 보여�??�이�???(10,20,30... ??)
   		int endpage = maxpage;
   		
   		if (endpage>startpage+10-1) endpage=startpage+10-1;
   		
   		request.setAttribute("page", page);		  //?�재 ?�이�?��  
   		request.setAttribute("maxpage", maxpage); //최�? ?�이�???
   		request.setAttribute("startpage", startpage); //?�재 ?�이�?�� ?�시??�??�이�?�� 
   		request.setAttribute("endpage", endpage);     //?�재 ?�이�?�� ?�시?????�이�???
		request.setAttribute("listcount",listcount); //�???
		request.setAttribute("Boardlist", list);
		
		ActionForward forward= new ActionForward();
	   	forward.setRedirect(false);
   		forward.setPath("BoardView.jsp");
   		return forward;
	}

}
